﻿using System;
using Ah.Testudin.Engine.UI.Elements;
using Microsoft.Xna.Framework;

namespace Ah.Testudin.Engine.Utilities
{
    public static class MathUtilities
    {
        public static float GetSignedAngle(
            Vector3 a,
            Vector3 b)
        {
            a.Normalize();

            b.Normalize();

            float theta = (float)Math.Acos(
                Vector3.Dot(
                    a,
                    b));

            Vector3 normal = Vector3.Normalize(
                Vector3.Cross(
                    b,
                    a));

            Vector3 v3 = Vector3.Normalize(
                Vector3.Cross(
                    a,
                    normal));

            if (Vector3.Dot(
                v3,
                b) > 0)
            {
                theta *= -1;
            }

            return theta;
        }

        public static Matrix GetLookAt(Vector3 position, Vector3 lookAt)
        {
            Matrix rotation = new Matrix();

            rotation.Forward = Vector3.Normalize(
                lookAt - position);

            rotation.Right = Vector3.Normalize(
                Vector3.Cross(
                    rotation.Forward,
                    Vector3.Up));

            rotation.Up = Vector3.Normalize(
                Vector3.Cross(
                    rotation.Right,
                    rotation.Forward));

            return rotation;
        }
    }
}
